﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hive.Core
{
    public class Ant
    {
        static int count = 0;
        public int Id { get; private set; }

        public Ant(Location loc)
        {
            Id = count;
            count++;
            Location = new Location(loc.Row, loc.Col);
        }
        public Location Location
        {
            get;
            private set;
        }

        private void Move(Direction direction)
        {
            Engine.IssueOrder(Location, direction);
            var destination = Env.Map.GetDestination(Location, direction);
            Log.Write(string.Format("Move ant [{0}] at {1} to {2}", Id, Location.ToString(), destination.ToString()));

            Location = destination;

            if (Env.Map[destination] == Tile.Water)
                Log.Write(string.Format("WATER!!! PIZDA!!!!"));
        }

        public void Move (Location destination)
        {
            try
            {
                var d = Env.Map.GetDirections(Location, destination);
                Move(d.First());
            }
            catch
            {
                Log.Write("Cannot get directions from {0} to {1}", Location, destination);
            }
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            var other = obj as Ant;
            if (other == null)
                return false;

            return this.Id == other.Id;
        }
    }
}
